package pkg

import (
	"context"
	"fmt"
	"log"
	"onerk4/zg5yk/ordering/common/global"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

const (
	BUCKET = "zg3test"
	REGION = "cn-shanghai"
)

func BigFileUpload(localFile, filename string) string {
	fmt.Println(localFile)
	aliYun := &global.AppConfig.Nacos.AliYun
	// 加载默认配置并设置凭证提供者和区域
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewStaticCredentialsProvider(aliYun.AccessKeyID, aliYun.AccessKeySecret)).
		WithRegion(REGION)

	// 创建OSS客户端
	client := oss.NewClient(cfg)

	// 创建上传管理器
	u := client.NewUploader()

	// 执行上传文件的操作
	result, err := u.UploadFile(context.TODO(),
		&oss.PutObjectRequest{
			Bucket: oss.Ptr(BUCKET),
			Key:    oss.Ptr(filename),
			Acl:    oss.ObjectACLPublicRead,
		},
		localFile)
	if err != nil {
		log.Fatalf("failed to upload file %v", err)
	}
	// 打印上传文件的结果
	log.Printf("upload file result:%#v\n", result)

	return fmt.Sprintf("https://%s.oss-cn-shanghai.aliyuncs.com/%s", BUCKET, filename)
}
